﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.UserSkins;
using DevExpress.XtraBars.Helpers;
using DevExpress.XtraEditors;
using System.Data.SqlClient;
using QLTV.BLL;
using QLTV.Public;

namespace QLTV.GUI.Forms
{
    public partial class frmPhieuThuTienPhat : DevExpress.XtraEditors.XtraForm
    {
        PhieuSachDTO psDTO = new PhieuSachDTO();
        PhieuSachBLL psBLL = new PhieuSachBLL();
        public frmPhieuThuTienPhat()
        {
            InitializeComponent();
        }

        public frmPhieuThuTienPhat(string madg, string maphieu, string tendg, string tongno, DateTime ngaytra, string songaymuon, decimal tienphatkynay)
        {
            InitializeComponent();
            txtMaDG.Text = madg;
            txtHoTen.Text = tendg;
            txtTongNo.Text = tongno;
            psDTO.MaPhieu = maphieu;
            psDTO.NgayTra = ngaytra;
            psDTO.SoNgayMuon = songaymuon;
            psDTO.TienPhatKyNay = tienphatkynay;
        }

        private void btnXacNhan_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtSoTienThu.Text.ToString()))
            {
                MessageBox.Show("Bạn phải nhập số tiền thu vào!");
            }
            else
            {
                psDTO.SoTienThu = decimal.Parse(txtSoTienThu.Text.ToString());
                psBLL.NhanTraSach(psDTO);
                XtraMessageBox.Show("Trả sách thành công!");
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        

        private void txtSoTienThu_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;

            if (string.IsNullOrEmpty(txtSoTienThu.Text.ToString()))
            {
                txtConLai.Text = "";
            }
            else
            {

                //txtConLai.Text = (decimal.Parse(txtTongNo.Text.ToString()) - decimal.Parse(txtSoTienThu.Text.ToString())).ToString();
                if (decimal.Parse(txtSoTienThu.Text.ToString()) > decimal.Parse(txtTongNo.Text.ToString()))
                {
                    e.Handled = false;
                    //this.txtSoTienThu
                    //XtraMessageBox.Show(txtSoTienThu.Text.ToString());

                    XtraMessageBox.Show("Số tiền thu không được lớn hơn tổng nợ!");
                    
                    txtSoTienThu.Text = txtTongNo.Text;
                    //txtSoTienThu.Text = txtSoTienThu.Text.Substring(1, 4);

                   // txtConLai.Text = (decimal.Parse(txtTongNo.Text.ToString()) - decimal.Parse(txtSoTienThu.Text.ToString())).ToString();
                }
               
                    txtConLai.Text = (decimal.Parse(txtTongNo.Text.ToString()) - decimal.Parse(txtSoTienThu.Text.ToString())).ToString();

            }
        }
    }
}